package com.test;

import com.cn.jpush.api.JPushClient;
import com.cn.jpush.api.common.resp.APIConnectionException;
import com.cn.jpush.api.common.resp.APIRequestException;
import com.cn.jpush.api.push.PushResult;
import com.cn.jpush.api.push.model.Message;
import com.cn.jpush.api.push.model.Options;
import com.cn.jpush.api.push.model.Platform;
import com.cn.jpush.api.push.model.PushPayload;
import com.cn.jpush.api.push.model.audience.Audience;
import com.cn.jpush.api.push.model.audience.AudienceTarget;
import com.cn.jpush.api.push.model.notification.IosNotification;
import com.cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by yangluo on 2015/7/9.
 */
public class JdPush {
    protected static final Logger LOG = LoggerFactory.getLogger(JdPush.class);

    // demo App defined in resources/jpush-api.conf

    public static  String TITLE;

    public static  String ALERT;

    public static  String MSG_CONTENT;

    public static  String REGISTRATION_ID;

    public static  String TAG ;

    public  static JPushClient jpushClient=null;

    //推送内容


    /**
     * 根据appkey masterSecret 推送消息
     * @param appKey
     * @param masterSecret
     */
    public static void testSendPush(String appKey ,String masterSecret) {
        jpushClient = new JPushClient(masterSecret, appKey, 3);
        // HttpProxy proxy = new HttpProxy("localhost", 3128);
        // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
        //推送所有用户
        PushPayload payload= buildPushObject_android_and_ios();
        try {
            System.out.println(payload.toString());
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result+"................................");
            LOG.info("Got result - " + result);
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
        }
    }
    //可以根据  使用平台进行对平台的统一推送消息
    //可以根据  使用者的REGISTRATION_ID 极光推送给的唯一标示进行推送消息 或者 根据别名 标签  进行推送
    /**
     * 推送所有消息
     * 快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知。
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert() {

        return PushPayload.alertAll(ALERT);
    }

    /**
     * 对所有用户进行推送消息
     * 构建推送对象：所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT。
     * Audience设置为all()，说明采用广播方式推送，所有用户都可以接收到
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
                .setNotification(Notification.alert(ALERT))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

    /**
     *  向安卓用户推送消息
     * @return
     */
    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId())
                .setNotification(Notification.android(ALERT, TITLE, null))
                .build();
    }

    /**
     *  向安卓和苹果用户推送消息
     * @return
     */
    public static PushPayload buildPushObject_android_and_ios() {
        return PushPayload.newBuilder()//入口
                .setPlatform(Platform.ios())//操作平台
                .setAudience(Audience.registrationId(REGISTRATION_ID))
                        .setNotification(Notification.newBuilder()
                                .setAlert(ALERT).build())
                .setMessage(Message.content(MSG_CONTENT))
                .build();
    }

    /**
     *  平台是 iOS，推送目标是 "tag1", "tag_all" 的交集，推送内容同时包括通知与消息 -
     *  通知信息是 ALERT，角标数字为 5，通知声音为 "happy"，并且附加字段 from = "JPush"；
     *  消息内容是 MSG_CONTENT。通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。
     *  APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）
     * @return
     */
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "tag_all"))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(5)
                                .setSound("happy.caf")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                .setMessage(Message.content(MSG_CONTENT))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

    /**
     * 向苹果用户推送多条消息
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(MSG_CONTENT)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }
    public  static  void main(String[] arge){
        //安卓appKey
        //a4bd9171fdd1272ad5a102da
        //安卓masterSecret
        //137ad5fb43f47a99abd94686


        //注册应用的主密码,即API 主密码

//         String appKey ="a4bd9171fdd1272ad5a102da";
//         String masterSecret = "137ad5fb43f47a99abd94686";



//        String appKey="3569fef92b1231f0859ad118";
//        String masterSecret="4a93b6a1d5eb5f0e67247abc";
        //调用方法进行推送消息
        JdPush.ALERT="欢迎您！使用花生米移动支付，祝您生活愉快 !";
        JdPush.MSG_CONTENT="花生末移动支付感恩大回馈";
        JdPush.TITLE="花生米移动支付";
        JdPush.REGISTRATION_ID="0017f5d77df";
        //JdPush.testSendPush("3569fef92b1231f0859ad118", "4a93b6a1d5eb5f0e67247abc");
        JdPush.testSendPush("69877d0acd6132c5f9182e4c", "3098e5c689958b8967a8e923");
    }
}
